package com.hpe.wx.performance.mongo.jmxObj.ctrl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpe.wx.performance.mongo.jmxObj.domain.JmxObj;
import com.hpe.wx.performance.mongo.jmxObj.service.JmxObjService;
import com.hpe.wx.performance.redis.utils.JMXUtils;

@Controller
@RequestMapping(value = "/hpe/performance/jmxUrl")
public class JmxObjCtrl {
	@Autowired
	private JmxObjService service;

	
	@ResponseBody
	@RequestMapping(value = "/findBy", method = RequestMethod.GET)
	public Map<String, Object> test(Locale locale, Model model, String jmxUrl) {
		System.out.println("findBy");
		JmxObj jo = service.getByName("test");
		System.out.println(jo);
		jo = service.getByUrl("");
		System.out.println(jo);
		List<JmxObj> jos = service.getByStatus(1); 
		System.out.println(jos);
		return null;
	}
	
	
	/**
	 * 主页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String pagingList(Model model) {
		model.addAttribute("TestAttribute", "欢迎进入");
		List<JmxObj> jmxUrls = service.getByStatus(1);
		model.addAttribute("jmxUrls", jmxUrls);
		return "hpe/performancemongo/jmxUrlAdmin";
	}

	@ResponseBody
	@RequestMapping(value = "/checkJmxUrl", method = RequestMethod.GET)
	public Map<String, Object> checkJmxUrl(Locale locale, Model model, String jmxUrl) {
		jmxUrl = jmxUrl.trim();
		Map<String, Object> res =  JMXUtils.checkJmxUrlUseable(jmxUrl);
		return res;
	}
	
	/**
	 * 添加一个jmxurl
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addDel", method = RequestMethod.GET)
	public Map<String, Object>  addDel(Model model, HttpServletRequest request, HttpServletResponse response,
			String jmxName, String jmxUrl, String userName, String password,String cmd) {
		//jmxUrls
		boolean isExist = true;
		Map<String, Object> res = new HashMap<>();
		List<JmxObj> jmxObjs = service.getByStatus(1);
		
		if("add".equals(cmd)){//add a jmx
			JmxObj joTest = new JmxObj(jmxName, jmxUrl);
			isExist = checkJmxObjIsExist(joTest, jmxObjs);
			if(isExist){
				res.put("status", "failed");
				res.put("message", "The #" + jmxName +" OR "+ jmxUrl+ "# is exist");
			}
				 
			if(!isExist){
				JmxObj jmx = new JmxObj(jmxName,jmxUrl ,new Date()); 
				jmx.setStatus(1);
				jmx.setCreatedBy(0l);
				jmx.setCreatedDate(new Date());
				service.addJmxUrl(jmx);
				jmxObjs = service.getByStatus(1);
				res.put("status", "success");
				res.put("message", "add jmx url successfully");
			}
		}else{//delete a jmx
			service.delByUrl(jmxUrl);
			jmxObjs = service.getByStatus(1);
			res.put("status", "success");
			res.put("message", "delete jmx url successfully");
		}

		model.addAttribute("jmxUrls", jmxObjs);
		
		return res;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public Map<String, Object>  update(Model model, HttpServletRequest request, HttpServletResponse response,String id,
			String jmxName, String jmxUrl, String userName, String password ) {
		//jmxUrls
		boolean isExist = true;
		Map<String, Object> res = new HashMap<>();
		
		JmxObj temp = service.getById(id);
		
		temp.setName(jmxName);
		temp.setUrl(jmxUrl);
		temp.setUserName(userName);
		temp.setPassword(password);
		temp.setLastModifiedBy(0l);
		temp.setLastModifiedDate(new Date());
		
		service.updateJmxUrl(temp);
		
		List<JmxObj> jmxObjs = service.getByStatus(1);
		

		model.addAttribute("jmxUrls", jmxObjs);
		
		res.put("status", "success");
		res.put("message", "update successfully");
		
		return res;
	}

	private boolean checkJmxObjIsExist(JmxObj joTest,List<JmxObj> jmxObjs){
		boolean success = false;
		for(JmxObj jo : jmxObjs){
			if(joTest.getUrl().equals(jo.getUrl()) || joTest.getName().equals(jo.getName())){
				success = false;
			}
		} 
		return success;
	}
}
